package com.ltlixer.bootadmin.usys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltlixer.bootadmin.api.dto.UserDto;
import com.ltlixer.bootadmin.api.service.UserService;
import com.ltlixer.bootadmin.usys.entity.CoreUser;
import com.ltlixer.bootadmin.usys.mapper.CoreUserMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Component
@Transactional
public class UserServiceImpl extends ServiceImpl<CoreUserMapper, CoreUser> implements UserService {

    @Override
    public List<UserDto> listUsers() {
        List<CoreUser> coreUsers = baseMapper.selectList(null);
        ArrayList<UserDto> userDtos = new ArrayList<>();
        UserDto userDto;
        for (CoreUser coreUser: coreUsers) {
            userDto = new UserDto();
            userDto.setUsername(coreUser.getId().toString());
            userDto.setMobile(coreUser.getAttachmentId());
            userDto.setSex("1");
            userDtos.add(userDto);
        }
        return userDtos;
    }

    @Override
    public void saveUser(UserDto user) {
        CoreUser coreUser = new CoreUser();
        coreUser.setName(user.getUsername());
        coreUser.setDelFlag(1);
        coreUser.setAttachmentId(user.getMobile());
        coreUser.setId((long) user.getUsername().hashCode());
        baseMapper.insert(coreUser);
    }
}
